<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends Action {
    
    public function index(){
        $this->title = '渝西团—英雄联盟在线报名';
        $this->keywords = '渝西团,英雄联盟,在线报名';
        $this->description = '渝西团—英雄联盟在线报名';
        $this->display('baoming');
    }

    /**
     * 保存报名信息
     */
    public function add()
    {

        if(isset($_FILES['avatar']['name']) && !empty($_FILES['avatar']['name']))
        {
            //$data['avatar'] = trim($_POST['mobile']);
            $uploadDir = 'Uploads/avatar/';
            $FileName = RandNumber().'.'.get_extension($_FILES['avatar']['name']); //生成16位长度的随机文件名
            $avatarPath =  $uploadDir.$FileName;
            if ($_FILES["avatar"]["error"] > 0)
            {
                $this->show(showMsg('上传异常 Error: '.$_FILES["avatar"]["error"],'-1',2000));
            }
            else
            {
                if(!is_dir($uploadDir))
                {
                    CreateDire($uploadDir);
                }
                $max_file_size = (1024 * 10000);
                if ((($_FILES["avatar"]["type"] == "image/gif") || ($_FILES["avatar"]["type"] == "image/jpeg") || ($_FILES["avatar"]["type"] == "image/pjpeg")) && ($_FILES["avatar"]["size"] < $max_file_size))
                {
                    if (file_exists($avatarPath))
                    {
                        $this->show(showMsg("很抱歉，文件 <font style='color:#F00;font-weight:bold'>".$_FILES["avatar"]["name"] ."</font> 已经存在",'-1',2000));
                    }
                    else
                    {
                        $UploadSuccess = move_uploaded_file($_FILES["avatar"]["tmp_name"], $avatarPath);
                        if($UploadSuccess)
                        {
                            $groups = M('groups');
                            $data['group_name'] = trim($_POST['group_name']);
                            $data['leader_name'] = trim($_POST['leader_name']);
                            $data['qq'] = trim($_POST['qq']);
                            $data['mobile'] = trim($_POST['mobile']);
                            $data['avatar'] = $avatarPath;
                            $data['ispayment'] = 0;
                            $data['pubdate'] = date('Y-m-d H:i:s',time());

                            $data['group_info'] = trim($_POST['group_info']);
                            $insert_id = $groups->add($data);
                            if($insert_id)
                            {
                                $fields = array('player_name','player_sex','player_age','player_idcard');
                                foreach ($fields as $key => $val) {
                                    foreach ($_POST[$val] as $k => $v) {
                                        $rows[$k][$val] = $v;
                                    }
                                }
                                foreach ($rows as $key => $value) {
                                    $players = M('players');
                                    $value['gid'] = $insert_id;
                                    $players->add($value);
                                }
                                $this->show(showMsg('报名成功','__URL__',2000));
                            }
                            else
                            {
                                $this->show(showMsg('数据添加失败','__URL__',2000));
                            }
                        }else{
                            $this->show(showMsg('战队头像上传失败','-1',2000));
                        }
                    }
                }
                else{
                    $this->show(showMsg('文件格式错误或大小超过限制','-1',2000));
                }
            }
        }
        else
        {
            $this->show(showMsg('战队头像不能为空','-1',2000));
        }
    }

	/**
     * 投票列表
     */
	public function votelist()
    {
	    $this->title = '渝西团—英雄联盟在线投票';
        $this->keywords = '渝西团,英雄联盟,在线投票';
        $this->description = '渝西团—英雄联盟在线投票';
        $groups = M('groups');
        $list = $groups->order('votenum DESC')->select();
        foreach ($list as $key => $val) {
            $arr[] = $val;
            $arr[$key]['rank'] = $key+1;
        }
        //echo '<script type="text/javascript">alert("'.$_SESSION['userid'].'")</script>';
        $this->assign('list',$arr);
        $this->display('votelist');
	}

    /**
     * 点击投票
     * @return [type] [description]
     */
    public function subvote()
    {
        session_start();
        if(!isset($_SESSION['uid'])||empty($_SESSION['uid'])){
            echo 'unlogin';
            exit();
        }
        $gid = empty($_REQUEST['gid'])?0:intval($_REQUEST['gid']);
        $vote = M('vote');
        $vtrow = $vote->where('gid='.$gid)->find();
        if($vtrow){
            $uid = $vtrow['uid'];
            $arr_uid = explode(",",$uid);
            $flag = false;
            if(is_array($arr_uid)){
                foreach($arr_uid as $key => $val)
                {
                    if($_SESSION['uid'] == $val){
                       $flag = true;
                       break;
                    }
                }
            }
            if($flag){
                echo 'exist';
            }
            else{
                $data['uid'] = $uid.$_SESSION['uid'].",";
                if($vote->where('gid='.$gid)->save($data)){
                    $groups = M('groups');
                    $votenum = $groups->where('gid='.$gid)->getField("votenum");
                    $votenum++;
                    $dt['votenum'] = $votenum;
                    $groups->where('gid='.$gid)->save($dt);
                    echo '2';
                }
            }
        }else{
            $data['uid'] = $_SESSION['uid'].",";
            $data['gid'] = $gid;
            if($vote->add($data)){
                $groups = M('groups');
                $votenum = $groups->where('gid='.$gid)->getField("votenum");
                $votenum++;
                $dt['votenum'] = $votenum;
                $groups->where('gid='.$gid)->save($dt);
                echo '1';
            }
        }
    }

    /**
     *检查用户名是否存在
     */
    public function checkuname(){
        $uname = trim($_REQUEST['uname']);
        $user = M('user');
        $dbrow = $user->where('uname="'.$uname.'"')->find();
        if($dbrow){
            echo 'exist';
        }
    }

    /**
     * 用户登录
     * @return [type] [description]
     */
    public function signin(){
        session_start();
        $uname = trim($_POST['uname']);
        $pwd = md5(trim($_POST['pwd']));
        $user = M('user');
        $rows = $user->where('uname="'.$uname.'" AND pwd = "'.$pwd.'"')->find();
        if($rows){
            $_SESSION['uid'] = $rows['uid'];
            $_SESSION['uname'] = $rows['uname'];
            $this->show(showMsg('登录成功','__URL__/votelist',2000));
        }else{
            $_SESSION['uid'] = "";
            $this->show(showMsg('登录失败','__URL__/votelist',2000));
        }
    }

    /**
     * 用户注册
     * @return [type] [description]
     */
    public function signup(){
        $data['uname'] = trim($_POST['uname']);
        $data['pwd'] = md5(trim($_POST['pwd']));
        $user = M("user");
        $dbrow = $user->where('uname="'.$data['uname'].'"')->find();
        if($dbrow){
            $this->show(showMsg('用户名已存在，请更改后再试','__URL__/votelist',2000));
        }
        else{
            $flag = $user->add($data);
            if($flag){
                $this->show(showMsg('注册成功','__URL__/votelist',2000));
            }
            else{
                $this->show(showMsg('注册失败，请稍后再试','__URL__/votelist',2000));
            }
        }

    }
    /**
     * 退出登录
     * @return [type] [description]
     */
    public function loginout(){
        unset($_SESSION['uid']);
        if(!isset($_SESSION['uid'])){
            $this->show(showMsg('注销成功','__URL__/votelist',2000));
        }
        else{
            $this->show(showMsg('注销失败，请稍后再试','__URL__/votelist',2000));
        }            
    }
}